/**
 * 
 */
package pl.com.like.sequoia.jms.connection;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import pl.com.sequoia.jms.JmsMessageData;
import pl.com.sequoia.jms.helper.IMessageObjectHandler;

/**
 * @author pwolanski
 * 
 */
@Component
public class JmsMessageListener implements MessageListener {

	private final static Logger log = LoggerFactory
			.getLogger(JmsMessageListener.class);
	private IMessageObjectHandler objectHandler;

	public IMessageObjectHandler getObjectHandler() {
		return objectHandler;
	}

	/** {@inheritDoc} */
	public void onMessage(Message message) {
		try {
			if (log.isDebugEnabled()) {
				log.debug("new message arrived!");
			}

			if (message instanceof MapMessage) {
				MapMessage mMsg = (MapMessage) message;
				Long upFileId = mMsg.getLong(JmsMessageData.UPLOAD_FILE_ID);
				if (log.isDebugEnabled()) {
					log.debug("new message have set id property as {}",
							upFileId);
				}
				objectHandler.proceed(upFileId);
			}
		} catch (JMSException e) {
			log.error(e.getMessage(), e);
		}

	}

	public void setObjectHandler(IMessageObjectHandler objectHandler) {
		this.objectHandler = objectHandler;
	}

}
